
package view;

import controller.FilmeController;
import javax.swing.JOptionPane;
import model.Filme;
import model.FilmesModel;

/**
 * @author Lucas Amaral
 */
public class FilmeView extends javax.swing.JFrame {
    private FilmeController controller;
    private FilmesModel model;
    private Filme filmeEditar;
    
    public FilmeView(FilmesModel m, Filme filme) {
        initComponents();
        model = m;
        controller = new FilmeController(this, model);
        if (filme != null) {
            jButtonSalvar.setVisible(false);
            filmeEditar = filme;
            jTextFieldNome.setText(filme.getNome());
            if (filme.getEmCartaz()) {
                jComboBoxEmCartaz.setSelectedIndex(0);
            } else {
                jComboBoxEmCartaz.setSelectedIndex(1);
            }
        } else {
            jLabelEmCartaz.setVisible(false);
            jComboBoxEmCartaz.setVisible(false);
            jButtonSalvarAlterações.setVisible(false);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelCabecalho = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jTextFieldNome = new javax.swing.JTextField();
        jButtonCancelar = new javax.swing.JButton();
        jLabelEmCartaz = new javax.swing.JLabel();
        jButtonSalvarAlterações = new javax.swing.JButton();
        jComboBoxEmCartaz = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelCabecalho.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabelCabecalho.setText("Cadastro filme");

        jLabelNome.setText("Nome");

        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabelEmCartaz.setText("Status");

        jButtonSalvarAlterações.setText("Salvar alterações");
        jButtonSalvarAlterações.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarAlteraçõesActionPerformed(evt);
            }
        });

        jComboBoxEmCartaz.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Em cartaz", "Fora de exibição" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldNome))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSalvarAlterações)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                        .addComponent(jButtonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCabecalho)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelEmCartaz)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBoxEmCartaz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelCabecalho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelEmCartaz)
                    .addComponent(jComboBoxEmCartaz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvar)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonSalvarAlterações))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        if (controller.inserirFilme(jTextFieldNome.getText(), true)) {
            JOptionPane.showMessageDialog(this, "Filme cadastrado com sucesso");
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Nome de filme já cadastrado, cadastre somente titulos diferente dos existentes");
        }
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonSalvarAlteraçõesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarAlteraçõesActionPerformed
        controller.alterarFilme(filmeEditar, jTextFieldNome.getText(), jComboBoxEmCartaz.getSelectedItem().toString());
        dispose();
    }//GEN-LAST:event_jButtonSalvarAlteraçõesActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FilmeView(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JButton jButtonSalvarAlterações;
    private javax.swing.JComboBox jComboBoxEmCartaz;
    private javax.swing.JLabel jLabelCabecalho;
    private javax.swing.JLabel jLabelEmCartaz;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JTextField jTextFieldNome;
    // End of variables declaration//GEN-END:variables
}
